<?php

/**
 * UploadForm class.
 * UploadForm is the data structure for keeping
 * web upload form data. It is used by the 'upload' action of 'OptionsController'.
 */
class UploadForm extends Options
{
    public $upload_max_size; //文件大小 : 10240KB
    public $upload_allow_ext; //文件类型:jpg,gif,bmp,jpeg,png,doc,zip,rar,7z,txt,sql,pdf
    public $upload_water_status; //水印开关,1:开启，2:关闭
    public $upload_water_file; //水印文件url
    public $upload_water_scope;  //图片范围,大于此尺寸的图片才会被打上水印:300x300
    public $upload_water_position;  //水印位置 0：右下 1：左上 2：左下 3：右上 4：中间 5：随机
    public $upload_water_padding; //水印边距
    public $upload_water_trans; //水印透明度,1－100的整数,越大透明度越低

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// upload_max_size and upload_allow_ext are required
			array('upload_max_size, upload_allow_ext', 'required'),
			// 验证字符串 string
            array('upload_max_size', 'numerical', 'integerOnly'=>true),
            /*array(
                'upload_allow_ext',
                'file',
                'types'=>array('jpg','gif','bmp','jpeg','png','doc','docx','xls','xlsx','zip','rar','7z','pdf'),
            ),*/
            array('upload_water_status', 'in', 'range'=>array(1,2)),
			array('upload_water_file', 'default', 'value'=>'resource/frontend/images/water.png'),
           array('upload_water_scope', 'default', 'value'=>'300x300'),
			array('upload_water_position', 'in', 'range'=>array(0,1,2,3,4,5)),
            array('upload_water_padding', 'default', 'value'=>'10x10'),
            array('upload_water_trans', 'numerical', 'integerOnly'=>true),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'upload_max_size' => Yii::t('backend', '文件大小'),
            'upload_allow_ext' => Yii::t('backend', '文件类型'),
            'upload_water_status' => Yii::t('backend', '水印状态'),
            'upload_water_file' => Yii::t('backend', '水印文件'),
            'upload_water_scope' => Yii::t('backend', '图片范围'),
            'upload_water_position' => Yii::t('backend', '水印位置'),
            'upload_water_padding' => Yii::t('backend', '水印边距'),
            'upload_water_trans' => Yii::t('backend', '水印透明度'),
		);
	}


    /**
     * 填充Upload配置
     */
    public function getUploadConfig()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name'=>$name));
            if ($model != null) {
                $this->$name = $model->value;
            } else {
                $this->$name = '';
            }
        }
    }


    /**
     * 更新Upload配置
     * @return bool
     * @throws CException
     */
    public function setUploadConfig()
    {
        $names = $this->getNames();
        foreach ($names as $name) {
            $model = self::model()->find('name=:name',array(':name'=>$name));
            if ($model != null) {
                $model->value = $_POST['UploadForm'][''.$name.''];
                $result = $model->save(false);
            } else {
                $model = Yii::createComponent( Options::className() );
                $model->name = $name;
                $model->value = $this->$name;
                $result = $model->save(false);
            }
            if ($result == false) {
                return $result;
            }
        }
        return true;
    }


}
